import { Component } from '@angular/core';
import { IonicPage,ViewController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../../providers/http/http';
import { StorageProvider } from '../../../providers/storage/storage';
import { DialogProvider } from '../../../providers/dialog/dialog';

/**
 * Generated class for the ModalprojectselectPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-modalprojectselect',
  templateUrl: 'modalprojectselect.html',
})
export class ModalprojectselectPage {
  private searchQuery: any;
	private listItems: Array<{
		id: any,
		code: any,
		name: any,
		unit: any,
		price: any,
		type: any,
		num: any,
		departname: any,
		assetBelong: any,
		buyDate: any,
		searched: any
	}>;
		constructor(
		public viewCtrl: ViewController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider) {

	}

  ionViewDidLoad() {
    console.log('ionViewDidLoad ModalprojectselectPage');
    this.loadData();
	}

	loadData() {
		this.storage.getStorage("user").then(val => {
			//请求方式如下
			let params = {
				user_id: val['user_id'],
				pageNo: 1
			};
			this.http.postJson('oa', 'officeSupplies/officeSuppliesSelect.do', params).then(res => {
				//返回数据之后,进行查询
				let datas = res['data']['name'];
				this.listItems = new Array();
				for (let i = 0; i < datas.length; i++) {
					let item = datas[i];
					this.listItems.push({
						id: item['id'],
						code: item['code'],
						name: item['name'],
						unit: item['unit'],
						type: item['type'],
						price: item['price'],
						num: item['num'],
						departname: item['manageDept'] == undefined ? '' : item['manageDept']['name'],
						assetBelong: item['assetBelong'],
						buyDate: item['buyDate'],
						searched: false
					});
				}
				//最终结果
				this.searchQuery = this.listItems;
			});
		});
	}

	//人物id以u_开头
	getItems(ev: any) {
		//输入关键字
		let val = ev.target.value;
		//查询
		if (val && val.trim() != '') {
			this.searchQuery = this.listItems.filter((item) => {
				return (item['name'].toLowerCase().indexOf(val.toLowerCase()) > -1);
			})
		}else{
			this.searchQuery = this.listItems;
		}
	}

	//关闭选择
	doclose() {
		this.viewCtrl.dismiss();
	}

	//确定选择
	doneselect() {
		//检查选择
		let datas = new Array();
		for (let i = 0; i < this.listItems.length; i++) {
			if (this.listItems[i]['searched'] == true) {
				datas.push(this.listItems[i]);
			}
		}
		//判断选择
		if (datas.length == 0) {
			this.dialog.showAlert('提示', '请选择项目');
		} else {
			let data = new Object();
			data['result'] = datas;
			this.viewCtrl.dismiss(data);
		}
	}

}
